using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using Portal.Core.DAL;

namespace Portal.BO.Editoral.BanDocViet
{
    public class Common
    {
        public Common() { }

        public DataTable GetListImage(string folderContainXml, string strId)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FileName", Type.GetType("System.String"));
            dt.Columns.Add("Description", Type.GetType("System.String"));


            XmlDocument doc = new XmlDocument();
            doc.Load(folderContainXml + strId + ".xml");
            XmlNodeList _currentImageNameNode = doc.SelectNodes("//ImageName");
            XmlNodeList _currentDescriptionNode = doc.SelectNodes("//Description");

            DataRow myDataRow;
            for (int i = 0; i < _currentImageNameNode.Count; i++)
            {
                if (_currentImageNameNode[i].InnerText.Trim() != "")
                {

                    myDataRow = dt.NewRow();
                    myDataRow["FileName"] = _currentImageNameNode[i].InnerText.Trim();
                    myDataRow["Description"] = _currentDescriptionNode[i].InnerText.Trim();
                    dt.Rows.Add(myDataRow);
                }
            }

            return dt;
        }

        public DataTable LoadFileIndexXmlToDataTable(string indexFile, string news_id)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);
            DataTable dt = new DataTable();
            dt.Columns.Add("Title", Type.GetType("System.String"));
            dt.Columns.Add("SendDate", Type.GetType("System.String"));
            dt.Columns.Add("Author_Email", Type.GetType("System.String"));
            dt.Columns.Add("Author_Name", Type.GetType("System.String"));
            dt.Columns.Add("Address", Type.GetType("System.String"));
            dt.Columns.Add("Phone", Type.GetType("System.String"));
            dt.Columns.Add("FileDetail", Type.GetType("System.String"));
            dt.Columns.Add("ID", Type.GetType("System.String"));
            dt.Columns.Add("CatID", Type.GetType("System.String"));
            dt.Columns.Add("CatName", Type.GetType("System.String"));

            XmlNodeList _currentTitleList = doc.SelectNodes("//Title");
            XmlNodeList _currentSendDateList = doc.SelectNodes("//SendDate");
            XmlNodeList _currentEmailList = doc.SelectNodes("//Author_Email");
            XmlNodeList _currentNameList = doc.SelectNodes("//Author_Name");
            XmlNodeList _currentAddressList = doc.SelectNodes("//Address");
            XmlNodeList _currentPhoneList = doc.SelectNodes("//Phone");
            XmlNodeList _currentFileDetailList = doc.SelectNodes("//FileDetail");
            XmlNodeList _currentNewsID = doc.SelectNodes("//NewsID");
            XmlNodeList _currentID = doc.SelectNodes("//ID");
            XmlNodeList _currentCatID = doc.SelectNodes("//CatID");

            DataRow myDataRow;
            for (int i = 0; i < _currentTitleList.Count; i++)
            {
                if (news_id.Trim() == "")
                {
                    if (_currentNewsID[i].InnerText.Trim() == "")
                    {
                        myDataRow = dt.NewRow();
                        myDataRow["Title"] = _currentTitleList[i].InnerText.Trim();
                        myDataRow["SendDate"] = Convert.ToDateTime(_currentSendDateList[i].InnerText.Trim()).ToString("dd/MM/yyyy HH:mm");
                        myDataRow["Author_Email"] = _currentEmailList[i].InnerText.Trim();
                        myDataRow["Author_Name"] = _currentNameList[i].InnerText.Trim();
                        myDataRow["Address"] = _currentAddressList[i].InnerText.Trim();
                        myDataRow["Phone"] = _currentPhoneList[i].InnerText.Trim();
                        myDataRow["FileDetail"] = _currentFileDetailList[i].InnerText.Trim();
                        myDataRow["ID"] = _currentID[i].InnerText.Trim();
                        myDataRow["CatID"] = _currentCatID[i].InnerText.Trim();
                        if (_currentCatID[i].InnerText.Trim() != "")
                        {
                            myDataRow["CatName"] = "";
                            if (StringProcess.IsNumber(_currentCatID[i].InnerText.Trim()))
                            {
                                CategoryRow objCat = Category.CategoryHelper.getCatInfoAsCategoryRow(Convert.ToInt32(_currentCatID[i].InnerText.Trim()));
                                myDataRow["CatName"] = objCat.Cat_Name != null ? objCat.Cat_Name : "";
                            }
                        }
                        else
                        {
                            myDataRow["CatName"] = "";
                        }
                        dt.Rows.Add(myDataRow);
                    }
                }
                else
                {
                    if (_currentNewsID[i].InnerText.Trim() == news_id.Trim())
                    {
                        myDataRow = dt.NewRow();
                        myDataRow["Title"] = _currentTitleList[i].InnerText.Trim();
                        myDataRow["SendDate"] = Convert.ToDateTime(_currentSendDateList[i].InnerText.Trim()).ToString("dd/MM/yyyy HH:mm");
                        myDataRow["Author_Email"] = _currentEmailList[i].InnerText.Trim();
                        myDataRow["Author_Name"] = _currentNameList[i].InnerText.Trim();
                        myDataRow["Address"] = _currentAddressList[i].InnerText.Trim();
                        myDataRow["Phone"] = _currentPhoneList[i].InnerText.Trim();
                        myDataRow["FileDetail"] = _currentFileDetailList[i].InnerText.Trim();
                        myDataRow["ID"] = _currentID[i].InnerText.Trim();
                        myDataRow["CatID"] = _currentCatID[i].InnerText.Trim();
                        dt.Rows.Add(myDataRow);
                    }
                }
                
                
            }
            return dt;
        }



        public DataTable LoadFileIndexXmlToDataTableByID(string indexFile, string strId)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);
            DataTable dt = new DataTable();
            dt.Columns.Add("Title", Type.GetType("System.String"));
            dt.Columns.Add("SendDate", Type.GetType("System.String"));
            dt.Columns.Add("Author_Email", Type.GetType("System.String"));
            dt.Columns.Add("Author_Name", Type.GetType("System.String"));
            dt.Columns.Add("Address", Type.GetType("System.String"));
            dt.Columns.Add("Phone", Type.GetType("System.String"));
            dt.Columns.Add("FileDetail", Type.GetType("System.String"));
            dt.Columns.Add("ID", Type.GetType("System.String"));

            XmlNodeList _currentTitleList = doc.SelectNodes("//Title");
            XmlNodeList _currentSendDateList = doc.SelectNodes("//SendDate");
            XmlNodeList _currentEmailList = doc.SelectNodes("//Author_Email");
            XmlNodeList _currentNameList = doc.SelectNodes("//Author_Name");
            XmlNodeList _currentAddressList = doc.SelectNodes("//Address");
            XmlNodeList _currentPhoneList = doc.SelectNodes("//Phone");
            XmlNodeList _currentFileDetailList = doc.SelectNodes("//FileDetail");
            XmlNodeList _currentNewsID = doc.SelectNodes("//NewsID");
            XmlNodeList _currentID = doc.SelectNodes("//ID");

            DataRow myDataRow;
            for (int i = 0; i < _currentTitleList.Count; i++)
            {
                if (strId.Trim() == "")
                {}
                else
                {
                    if (_currentID[i].InnerText.Trim() == strId.Trim())
                    {
                        myDataRow = dt.NewRow();
                        myDataRow["Title"] = _currentTitleList[i].InnerText.Trim();
                        myDataRow["SendDate"] = Convert.ToDateTime(_currentSendDateList[i].InnerText.Trim()).ToString("dd/MM/yyyy HH:mm");
                        myDataRow["Author_Email"] = _currentEmailList[i].InnerText.Trim();
                        myDataRow["Author_Name"] = _currentNameList[i].InnerText.Trim();
                        myDataRow["Address"] = _currentAddressList[i].InnerText.Trim();
                        myDataRow["Phone"] = _currentPhoneList[i].InnerText.Trim();
                        myDataRow["FileDetail"] = _currentFileDetailList[i].InnerText.Trim();
                        myDataRow["ID"] = _currentID[i].InnerText.Trim();
                        dt.Rows.Add(myDataRow);
                    }
                }


            }
            return dt;
        }


        public DataTable LoadFileBackUpXmlToDataTableByID(string indexFile, string strNewsID, string strFeedBackId)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);
            DataTable dt = new DataTable();
            dt.Columns.Add("UserName", Type.GetType("System.String"));
            dt.Columns.Add("Email", Type.GetType("System.String"));
            dt.Columns.Add("Phone", Type.GetType("System.String"));
            dt.Columns.Add("Address", Type.GetType("System.String"));


            XmlNodeList _currentNewsIDList = doc.SelectNodes("//NewsID");
            XmlNodeList _currentFeedBackIDList = doc.SelectNodes("//FeedBackID");
            XmlNodeList _currentEmailList = doc.SelectNodes("//Email");
            XmlNodeList _currentNameList = doc.SelectNodes("//UserName");
            XmlNodeList _currentAddressList = doc.SelectNodes("//Address");
            XmlNodeList _currentPhoneList = doc.SelectNodes("//Phone");

            DataRow myDataRow;
            for (int i = 0; i < _currentNameList.Count; i++)
            {
                if (strNewsID != "")
                {
                    if (_currentNewsIDList[i].InnerText.Trim() == strNewsID.Trim())
                    {
                        myDataRow = dt.NewRow();
                        myDataRow["Email"] = _currentEmailList[i].InnerText.Trim();
                        myDataRow["UserName"] = _currentNameList[i].InnerText.Trim();
                        myDataRow["Address"] = _currentAddressList[i].InnerText.Trim();
                        myDataRow["Phone"] = _currentPhoneList[i].InnerText.Trim();
                        dt.Rows.Add(myDataRow);
                    }
                }
                else
                {
                    if (_currentFeedBackIDList[i].InnerText.Trim() == strFeedBackId.Trim())
                    {
                        myDataRow = dt.NewRow();
                        myDataRow["Email"] = _currentEmailList[i].InnerText.Trim();
                        myDataRow["UserName"] = _currentNameList[i].InnerText.Trim();
                        myDataRow["Address"] = _currentAddressList[i].InnerText.Trim();
                        myDataRow["Phone"] = _currentPhoneList[i].InnerText.Trim();
                        dt.Rows.Add(myDataRow);
                    }
                }
            }
            return dt;
        }


        public string CountBaiBanDocViet(string indexFile)
        {
            int iReturn = 0;
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);
            
            XmlNodeList _currentTitleList = doc.SelectNodes("//Title");
            XmlNodeList _currentNewsID = doc.SelectNodes("//NewsID");
            
            for (int i = 0; i < _currentTitleList.Count; i++)
            {
                if (_currentNewsID[i].InnerText.Trim() == "")
                {
                    iReturn++;
                }
            }
            return iReturn.ToString();
        }


        public string CountBaiThaoLuan(string indexFile)
        {
            int iReturn = 0;
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);

            XmlNodeList _currentTitleList = doc.SelectNodes("//Title");
            XmlNodeList _currentNewsID = doc.SelectNodes("//NewsID");

            for (int i = 0; i < _currentTitleList.Count; i++)
            {
                if (_currentNewsID[i].InnerText.Trim() != "")
                {
                    iReturn++;
                }
            }
            return iReturn.ToString();
        }


        public DataTable GetListNewsID(string newsIdFile, string title, string cat_id)
        {
            string indexFile = System.Configuration.ConfigurationSettings.AppSettings["indexFile"];

            XmlDocument doc = new XmlDocument();
            doc.Load(newsIdFile);
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", Type.GetType("System.String"));
            dt.Columns.Add("CatID", Type.GetType("System.String"));
            dt.Columns.Add("News_Title", Type.GetType("System.String"));
            dt.Columns.Add("Cat_Name", Type.GetType("System.String"));
            dt.Columns.Add("SoBai", Type.GetType("System.String"));

            DataRow myDataRow;

            XmlNodeList _id = doc.SelectNodes("//ID");
            XmlNodeList _catid = doc.SelectNodes("//CatID");
            DataTable dt1;
            string news_title = "";
            bool isOk = true;
            Portal.Core.DAL.CategoryRow objCat = null;
            for (int i = 0; i < _id.Count; i++)
            {
                isOk = true;
                myDataRow = dt.NewRow();
                
                dt1 = Portal.BO.Editoral.Newsedit.NewsEditHelper.GetNewsById(_id[i].InnerText.Trim());
                if (dt1.Rows.Count > 0)
                {
                    news_title = dt1.Rows[0]["News_Title"].ToString();
                    myDataRow["News_Title"] = news_title;
                }
                else
                    myDataRow["News_Title"] = "";

                if (_catid[i].InnerText.Trim() != "")
                {
                    objCat = Portal.BO.Editoral.Category.CategoryHelper.getCatInfoAsCategoryRow(Convert.ToInt32(_catid[i].InnerText.Trim()));
                    if (objCat != null)
                        myDataRow["Cat_Name"] = objCat.Cat_Name;
                }
                else
                    myDataRow["Cat_Name"] = "";

                myDataRow["SoBai"] = CountNewsId(indexFile, _id[i].InnerText.Trim());


                if (title != "" && news_title.ToLower().Trim().IndexOf(title.ToLower().Trim()) == -1)
                {
                    isOk = false;
                }
                if (cat_id != "0" && cat_id != "" && cat_id != _catid[i].InnerText.Trim())
                {
                    isOk = false;
                }

                myDataRow["ID"] = _id[i].InnerText.Trim();
                myDataRow["CatID"] = _catid[i].InnerText.Trim();

                if(isOk == true)
                    dt.Rows.Add(myDataRow);
            }
            return dt;
        }

        public string GetFileDetailByID(string indexFile,string strID)
        {
            string strReturn = "";
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);

            XmlNodeList _currentFileDetailList = doc.SelectNodes("//FileDetail");
            XmlNodeList _currentID = doc.SelectNodes("//ID");
            for (int i = 0; i < _currentFileDetailList.Count; i++)
            {
                if (_currentID[i].InnerText.Trim() == strID)
                {
                    strReturn = _currentFileDetailList[i].InnerText.Trim();
                    break;
                }
            }

            return strReturn;
        }


        public string GetCatIDByID(string indexFile, string strID)
        {
            string strReturn = "";
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);

            XmlNodeList _currentCatIDList = doc.SelectNodes("//CatID");
            XmlNodeList _currentID = doc.SelectNodes("//ID");
            for (int i = 0; i < _currentID.Count; i++)
            {
                if (_currentID[i].InnerText.Trim() == strID)
                {
                    strReturn = _currentCatIDList[i].InnerText.Trim();
                    break;
                }
            }

            return strReturn;
        }

        

        public string GetNewsIdByID(string indexFile, string strID)
        {
            string strReturn = "";
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);

            
            XmlNodeList _currentID = doc.SelectNodes("//ID");
            XmlNodeList _currentNewsID = doc.SelectNodes("//NewsID");
            for (int i = 0; i < _currentID.Count; i++)
            {
                if (_currentID[i].InnerText.Trim() == strID)
                {
                    strReturn = _currentNewsID[i].InnerText.Trim();
                }
            }

            return strReturn;
        }

        public bool IsFeedBackForNews(string indexFile, string strID)
        {
            bool isReturn = false;
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);

            XmlNodeList _currentFileDetailList = doc.SelectNodes("//FileDetail");
            XmlNodeList _currentID = doc.SelectNodes("//ID");
            XmlNodeList _currentNewsID = doc.SelectNodes("//NewsID");
            for (int i = 0; i < _currentFileDetailList.Count; i++)
            {
                if (_currentID[i].InnerText.Trim() == strID.Trim())
                {
                    if (_currentNewsID[i].InnerText.Trim() != "")
                    {
                        isReturn = true;
                        break;
                    }
                }
                
            }

            return isReturn;
        }


        public void Delete(string strFileDetail)
        {
            string indexFile = System.Configuration.ConfigurationSettings.AppSettings["indexFile"];
            string folderContainXml = System.Configuration.ConfigurationSettings.AppSettings["folderContainXml"];
            string uploadFolder = System.Configuration.ConfigurationSettings.AppSettings["uploadFolder"];

            // Xoa trong file index xml
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);
            XmlNode _newsListNode = doc.SelectSingleNode("//NewsList");
            XmlNodeList _fileDetailNode = doc.SelectNodes("//FileDetail");
            XmlNodeList _newsNode = doc.SelectNodes("//News");
            for (int i = 0; i < _fileDetailNode.Count; i++)
            {
                if (_fileDetailNode[i].InnerText == strFileDetail)
                {
                    _newsListNode.RemoveChild(_newsNode[i]);
                    break;
                }
            }

            // Delete file xml and zip (contain images and video)
            if (File.Exists(uploadFolder + strFileDetail + ".zip"))
            {
                File.Delete(uploadFolder + strFileDetail + ".zip");
            }

            if (File.Exists(folderContainXml + strFileDetail + ".xml"))
            {
                File.Delete(folderContainXml + strFileDetail + ".xml");
            }

            doc.Save(indexFile);
        }

        public string CountNewsId(string indexFile, string strNewsID)
        {
            int isReturn = 0;
            XmlDocument doc = new XmlDocument();
            doc.Load(indexFile);

            XmlNodeList _currentFileDetailList = doc.SelectNodes("//FileDetail");
            XmlNodeList _currentNewsID = doc.SelectNodes("//NewsID");
            for (int i = 0; i < _currentFileDetailList.Count; i++)
            {
                if (_currentNewsID[i].InnerText.Trim() == strNewsID.Trim())
                {
                    if (_currentNewsID[i].InnerText.Trim() != "")
                    {
                        isReturn++;
                    }
                }

            }

            return isReturn.ToString();
        }

        public string GetLastestNewsID()
        {
            DataTable dt = new DataTable();
            using (MainDB objDb = new MainDB())
            {
                dt = objDb.SelectQuery("Select top 1 News_ID From News Order by News_CreateDate DESC ");
            }
            if (dt.Rows.Count > 0)
                return dt.Rows[0][0].ToString();

            return "";
        }

        public string GetLastestFeedBack_ID()
        {
            DataTable dt = new DataTable();
            using (MainDB objDb = new MainDB())
            {
                dt = objDb.SelectQuery("select max(FeedBack_ID) from FeedBackNews ");
            }
            if (dt.Rows.Count > 0)
                return dt.Rows[0][0].ToString();

            return "";
        }

    }
}
